package biz_struct

import "fmt"

// Node 链表的节点定义
type Node struct {
	Data interface{}
	Next *Node
}

// ShowNode 链表的遍历
func ShowNode(head *Node) {
	for head != nil {
		fmt.Println("节点内容", *head)
		head = head.Next
	}
}

// FindTailNode 链表的尾节点
func FindTailNode(head *Node) *Node {
	for head != nil {
		if head.Next == nil {
			return head
		}
		head = head.Next
	}
	return head
}

// TailInsert 尾插法, 返回头节点
func TailInsert() (head *Node) {
	head = &Node{Data: 0}
	var tail *Node
	tail = head
	arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
	for _, v := range arr {
		fmt.Println("需要插入的数据", v)
		current := &Node{Data: v}
		(*tail).Next = current
		tail = current
	}
	return
}

// TailInsertOneNode 尾插法, head ,tail, insertNode 参数, 返回头节点 head 和 尾节点 tail （头节点利于遍历，尾节点利用新增数据）
func TailInsertOneNode(head, tail, insertNode *Node) (*Node, *Node) {
	if head == nil || tail == nil {
		fmt.Println("head或者tail节点为空了")
		return head, tail
	}
	if insertNode == nil {
		fmt.Println("insertNode节点为空了")
		return head, tail
	}
	fmt.Println("需要插入的数据", insertNode.Data)
	(*tail).Next = insertNode
	tail = insertNode
	return head, tail
}
